home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / perl / os2perl / a2p.man < prev    next >
Text File  |  1991-06-18  |  6KB  |  163 lines

  1.  
  2.  
  3.  
  4.  
  5. LOCAL                                                      A2P(1)
  6.  
  7.  
  8.  
  9. NAME
  10.      a2p - Awk to Perl translator
  11.  
  12. SYNOPSIS
  13.      a2p [options] filename
  14.  
  15. DESCRIPTION
  16.      A2p takes an awk script specified on the command line (or
  17.      from standard input) and produces a comparable perl script
  18.      on the standard output.
  19.  
  20.      Options
  21.  
  22.      Options include:
  23.  
  24.      -D<number>
  25.           sets debugging flags.
  26.  
  27.      -F<character>
  28.           tells a2p that this awk script is always invoked with
  29.           this -F switch.
  30.  
  31.      -n<fieldlist>
  32.           specifies the names of the input fields if input does
  33.           not have to be split into an array.  If you were
  34.           translating an awk script that processes the password
  35.           file, you might say:
  36.  
  37.                a2p -7 -nlogin.password.uid.gid.gcos.shell.home
  38.  
  39.           Any delimiter can be used to separate the field names.
  40.  
  41.      -<number>
  42.           causes a2p to assume that input will always have that
  43.           many fields.
  44.  
  45.      Considerations
  46.  
  47.      A2p cannot do as good a job translating as a human would,
  48.      but it usually does pretty well.  There are some areas where
  49.      you may want to examine the perl script produced and tweak
  50.      it some.  Here are some of them, in no particular order.
  51.  
  52.      There is an awk idiom of putting int() around a string
  53.      expression to force numeric interpretation, even though the
  54.      argument is always integer anyway.  This is generally
  55.      unneeded in perl, but a2p can't tell if the argument is
  56.      always going to be integer, so it leaves it in.  You may
  57.      wish to remove it.
  58.  
  59.      Perl differentiates numeric comparison from string com-
  60.      parison.  Awk has one operator for both that decides at run
  61.      time which comparison to do.  A2p does not try to do a com-
  62.      plete job of awk emulation at this point.  Instead it
  63.      guesses which one you want.  It's almost always right, but
  64.      it can be spoofed.  All such guesses are marked with the
  65.      comment "#???".  You should go through and check them.  You
  66.      might want to run at least once with the -w switch to perl,
  67.      which will warn you if you use == where you should have used
  68.      eq.
  69.  
  70.      Perl does not attempt to emulate the behavior of awk in
  71.      which nonexistent array elements spring into existence sim-
  72.      ply by being referenced.  If somehow you are relying on this
  73.      mechanism to create null entries for a subsequent for...in,
  74.      they won't be there in perl.
  75.  
  76.      If a2p makes a split line that assigns to a list of vari-
  77.      ables that looks like (Fld1, Fld2, Fld3...) you may want to
  78.      rerun a2p using the -n option mentioned above.  This will
  79.      let you name the fields throughout the script.  If it splits
  80.      to an array instead, the script is probably referring to the
  81.      number of fields somewhere.
  82.  
  83.      The exit statement in awk doesn't necessarily exit; it goes
  84.      to the END block if there is one.  Awk scripts that do con-
  85.      tortions within the END block to bypass the block under such
  86.      circumstances can be simplified by removing the conditional
  87.      in the END block and just exiting directly from the perl
  88.      script.
  89.  
  90.      Perl has two kinds of array, numerically-indexed and associ-
  91.      ative.  Awk arrays are usually translated to associative
  92.      arrays, but if you happen to know that the index is always
  93.      going to be numeric you could change the {...} to [...].
  94.      Iteration over an associative array is done using the keys()
  95.      function, but iteration over a numeric array is NOT.  You
  96.      might need to modify any loop that is iterating over the
  97.      array in question.
  98.  
  99.      Awk starts by assuming OFMT has the value %.6g.  Perl starts
  100.      by assuming its equivalent, $#, to have the value %.20g.
  101.      You'll want to set $# explicitly if you use the default
  102.      value of OFMT.
  103.  
  104.      Near the top of the line loop will be the split operation
  105.      that is implicit in the awk script.  There are times when
  106.      you can move this down past some conditionals that test the
  107.      entire record so that the split is not done as often.
  108.  
  109.      For aesthetic reasons you may wish to change the array base
  110.      $[ from 1 back to perl's default of 0, but remember to
  111.      change all array subscripts AND all substr() and index()
  112.      operations to match.
  113.  
  114.      Cute comments that say "# Here is a workaround because awk
  115.      is dumb" are passed through unmodified.
  116.  
  117.      Awk scripts are often embedded in a shell script that pipes
  118.      stuff into and out of awk.  Often the shell script wrapper
  119.      can be incorporated into the perl script, since perl can
  120.      start up pipes into and out of itself, and can do other
  121.      things that awk can't do by itself.
  122.  
  123.      Scripts that refer to the special variables RSTART and
  124.      RLENGTH can often be simplified by referring to the vari-
  125.      ables $`, $& and $', as long as they are within the scope of
  126.      the pattern match that sets them.
  127.  
  128.      The produced perl script may have subroutines defined to
  129.      deal with awk's semantics regarding getline and print.
  130.      Since a2p usually picks correctness over efficiency.  it is
  131.      almost always possible to rewrite such code to be more effi-
  132.      cient by discarding the semantic sugar.
  133.  
  134.      For efficiency, you may wish to remove the keyword from any
  135.      return statement that is the last statement executed in a
  136.      subroutine.  A2p catches the most common case, but doesn't
  137.      analyze embedded blocks for subtler cases.
  138.  
  139.      ARGV[0] translates to $ARGV0, but ARGV[n] translates to
  140.      $ARGV[$n].  A loop that tries to iterate over ARGV[0] won't
  141.      find it.
  142.  
  143. ENVIRONMENT
  144.      A2p uses no environment variables.
  145.  
  146. AUTHOR
  147.      Larry Wall <lwall@jpl-devvax.Jpl.Nasa.Gov>
  148.  
  149. FILES
  150. SEE ALSO
  151.      perl The perl compiler/interpreter
  152.      s2p  sed to perl translator
  153.  
  154. DIAGNOSTICS
  155. BUGS
  156.      It would be possible to emulate awk's behavior in selecting
  157.      string versus numeric operations at run time by inspection
  158.      of the operands, but it would be gross and inefficient.
  159.      Besides, a2p almost always guesses right.
  160.  
  161.      Storage for the awk syntax tree is currently static, and can
  162.      run out.
  163.